package com.dao.impl;

import com.dao.BaseDeo;
import com.dao.IntegrationPackageDao;
import com.entity.composite.DeveloperInfo;
import com.entity.composite.IntegrationPackageDownloadInfo;
import com.entity.composite.IntegrationPackageInfo;
import com.entity.element_relation.Element;
import com.entity.short_reviews.IntegrationPackageShortReviews;
import com.entity.version_relation.Version;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wang_
 * @version 2024.3.4.1
 * @description
 * @date 2025/7/15
 */
public class IntegrationPackageDaoImpl extends BaseDeo implements IntegrationPackageDao {
	/// 获取精选整合包
	@Override
	public List<IntegrationPackageInfo> getSelectedIntegrationPackages() {
		linkConn();
		List<IntegrationPackageInfo> list = new ArrayList<>();
		//language=MySQL
		String sql = "select * from integration_package m inner join main_element me on m.main_element_id = me.id limit 3 offset 0";
		ResultSet rs = exeQuery(sql);
		if (rs == null) {
			return list;
		}
		try {
			list = getIntegrationPackageList(rs);
		} catch (SQLException e) {
			LOG.error("获取精选整合包异常！");
			LOG.error(e);
		}
		closeAll(rs);
		return list;
	}
	
	/// 获取整合包的基本信息
	@Override
	public IntegrationPackageInfo getBasicInformation(int integrationPackageId) {
		linkConn();
		IntegrationPackageInfo integrationPackage = new IntegrationPackageInfo();
		//language=MySQL
		String sql = "select * from integration_package m inner join main_element me on m.main_element_id = me.id where m.id=?";
		ResultSet rs = exeQuery(sql, integrationPackageId);
		if (rs == null) {
			return integrationPackage;
		}
		try {
			integrationPackage = getIntegrationPackageList(rs).get(0);
		} catch (SQLException e) {
			LOG.error("获取整合包基本信息异常！integrationPackageId={}", integrationPackageId);
			LOG.error(e);
		}
		closeAll(rs);
		return integrationPackage;
	}
	
	/// 获取整合包开发者
	@Override
	public List<DeveloperInfo> getDeveloper(int integrationPackageId) {
		linkConn();
		List<DeveloperInfo> list = new ArrayList<>();
		//language=MySQL
		String sql = " select * from integration_package_developer_relation mdr inner join developers d on mdr.developer_id = d.id where integration_package_id=?;";
		ResultSet rs = exeQuery(sql, integrationPackageId);
		if (rs == null) {
			return list;
		}
		try {
			while (rs.next()) {
				DeveloperInfo developers = new DeveloperInfo();
				developers.setCover(rs.getString("d.cover"));
				developers.setName(rs.getString("d.name"));
				developers.setDescription(rs.getString("d.description"));
				developers.setTeam(rs.getInt("d.team") != 0);
				developers.setId(rs.getInt("d.id"));
				developers.setRelation(rs.getString("mdr.relation"));
				list.add(developers);
			}
		} catch (SQLException e) {
			LOG.error("获取整合包开发者信息异常！integrationPackageId={}", integrationPackageId);
			LOG.error(e);
		}
		closeAll(rs);
		return list;
	}
	
	/// 获取整合包元素
	@Override
	public List<Element> getElement(int integrationPackageId) {
		linkConn();
		List<Element> list = new ArrayList<>();
		//language=MySQL
		String sql = "SELECT * FROM integration_package_element_relation mer inner join element e on mer.element_id = e.id where mer.integration_package_id = ?";
		ResultSet rs = exeQuery(sql, integrationPackageId);
		if (rs == null) {
			return list;
		}
		try {
			while (rs.next()) {
				Element element = new Element();
				element.setId(rs.getInt("e.id"));
				element.setName(rs.getString("e.name"));
				list.add(element);
			}
		} catch (SQLException e) {
			LOG.error("获取整合包元素异常！integrationPackageId={}", integrationPackageId);
			LOG.error(e);
		}
		closeAll(rs);
		return list;
	}
	
	/// 获取整合包版本
	@Override
	public List<Version> getVersion(int integrationPackageId) {
		linkConn();
		List<Version> list = new ArrayList<>();
		//language=MySQL
		String sql = "SELECT * FROM version v inner join integration_package_version_relation mvr on v.id = mvr.version_id where mvr.integration_package_id = ?";
		ResultSet rs = exeQuery(sql, integrationPackageId);
		if (rs == null) {
			return list;
		}
		try {
			while (rs.next()) {
				Version element = new Version();
				element.setId(rs.getInt("v.id"));
				element.setName(rs.getString("v.name"));
				list.add(element);
			}
		} catch (SQLException e) {
			LOG.error("获取整合包版本异常！integrationPackageId={}", integrationPackageId);
			LOG.error(e);
		}
		closeAll(rs);
		return list;
	}
	
	/// 获取整合包短评
	@Override
	public List<IntegrationPackageShortReviews> getShortReviews(int integrationPackageId) {
		linkConn();
		List<IntegrationPackageShortReviews> list = new ArrayList<>();
		//language=MySQL
		String sql = "select * from integration_package_short_reviews where integration_package_id = ?";
		ResultSet rs = exeQuery(sql, integrationPackageId);
		if (rs == null) {
			return list;
		}
		try {
			while (rs.next()) {
				IntegrationPackageShortReviews element = new IntegrationPackageShortReviews();
				element.setId(rs.getInt("id"));
				element.setFatherId(rs.getInt("father_id"));
				element.setPublisherId(rs.getInt("publisher_id"));
				element.setContent(rs.getString("content"));
				element.setLikeCount(rs.getInt("like_count"));
				element.setDate(rs.getString("date"));
				list.add(element);
			}
		} catch (SQLException e) {
			LOG.error("获取整合包短评信息异常！integrationPackageId={}", integrationPackageId);
			LOG.error(e);
		}
		closeAll(rs);
		return list;
	}
	
	/// 获取整合包下载
	@Override
	public List<IntegrationPackageDownloadInfo> getDownload(int integrationPackageId) {
		linkConn();
		List<IntegrationPackageDownloadInfo> list = new ArrayList<>();
		//language=MySQL
		String sql = "SELECT * FROM integration_package_download md inner join version v on md.version_id = v.id WHERE md.integration_package_id = ?";
		ResultSet rs = exeQuery(sql, integrationPackageId);
		if (rs == null) {
			return list;
		}
		try {
			while (rs.next()) {
				IntegrationPackageDownloadInfo download = new IntegrationPackageDownloadInfo();
				download.setId(rs.getInt("md.id"));
				download.setIntegrationPackageId(integrationPackageId);
				download.setLink(rs.getString("md.link"));
				download.setName(rs.getString("md.name"));
				download.setVersionId(rs.getInt("md.version_id"));
				download.setVersionName(rs.getString("v.name"));
				list.add(download);
			}
		} catch (SQLException e) {
			LOG.error("获取整合包下载信息异常！integrationPackageId={}", integrationPackageId);
			LOG.error(e);
		}
		closeAll(rs);
		return list;
	}
	
	private static List<IntegrationPackageInfo> getIntegrationPackageList(ResultSet rs) throws SQLException {
		List<IntegrationPackageInfo> list = new ArrayList<>();
		while (rs.next()) {
			IntegrationPackageInfo integrationPackage = new IntegrationPackageInfo();
			integrationPackage.setId(rs.getInt("m.id"));
			integrationPackage.setName(rs.getString("m.name"));
			integrationPackage.setCover(rs.getString("m.cover"));
			integrationPackage.setIntroductionInfo(rs.getString("m.introduction_info"));
			integrationPackage.setTotalViews(rs.getInt("m.total_views"));
			integrationPackage.setFavoriteCount(rs.getInt("m.favorite_count"));
			integrationPackage.setLikeCount(rs.getInt("m.like_count"));
			integrationPackage.setDate(rs.getString("m.date"));
			integrationPackage.setMainElementId(rs.getInt("m.main_element_id"));
			integrationPackage.setReviewStatusId(rs.getInt("m.review_status_id"));
			integrationPackage.setMainElementName(rs.getString("me.name"));
			list.add(integrationPackage);
		}
		return list;
	}
}
